
class matrix_count:
    def __init__(self, keys,fx=None):
        self.d=keys
        self.total=0
        self.fx=fx
        for i in self.d:
            for j in self.d:
                if i < j:
                    self.total=self.total+1
    def oncalc(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:

                        if b[i]>b[j]:
                            n=n+1
                        else:
                            n=n-1
            if self.fx=="buy":
                if n<0:
                    n=0
            elif self.fx=="sell":
                if n>0:
                    n=0
            return n/self.total
        else:
            return None


class matrix_count_up:
    def __init__(self, keys):
        self.d=keys
        self.total=0
        for i in self.d:
            for j in self.d:
                if i < j:
                    self.total=self.total+1
    def oncalc(self,b,timekey=None):
        if b[self.d[-1]]:
            n=0
            for i in self.d:
                for j in self.d:
                    if i<j:
                        if b[i]>b[j]:
                            n=n+1
            return n/self.total
        else:
            return None
